.TH scanf 3 "" "" ""
.SH SYNOPSIS
scanf, fscanf, sscanf \- scan and format input
.SH ANSI_SYNOPSIS
#include <stdio.h>
.br

int scanf(const char *
.IR format 
[, 
.IR arg ,
...]);
.br
int fscanf(FILE *
.IR fd ,
const char *
.IR format 
[, 
.IR arg ,
...]);
.br
int sscanf(const char *
.IR str ,
const char *
.IR format 
.br
[, 
.IR arg ,
...]);
.br
.SH TRAD_SYNOPSIS
#include <stdio.h>
.br

int scanf(
.IR format 
[, 
.IR arg ,
...])
.br
char *
.IR format ;
.br

int fscanf(
.IR fd ,
.IR format 
[, 
.IR arg ,
...]);
.br
FILE *
.IR fd ;
.br
char *
.IR format ;
.br

int sscanf(
.IR str ,
.IR format 
[, 
.IR arg ,
...]);
.br
char *
.IR str ;
.br
char *
.IR format ;
.br
.SH DESCRIPTION
.BR scanf 
scans a series of input fields from standard input,
one character at a time. Each field is interpreted according to
a format specifier passed to 
.BR scanf 
in the format string at
.BR *<[format >>.
.BR scanf 
stores the interpreted input from
each field at the address passed to it as the corresponding argument
following 
.IR format .
You must supply the same number of
format specifiers and address arguments as there are input fields.

There must be sufficient address arguments for the given format
specifiers; if not the results are unpredictable and likely
disasterous. Excess address arguments are merely ignored.

.BR scanf 
often produces unexpected results if the input diverges from
an expected pattern. Since the combination of 
.BR gets 
or 
.BR fgets 
followed by 
.BR sscanf 
is safe and easy, that is the preferred way
to be certain that a program is synchronized with input at the end
of a line.

.BR fscanf 
and 
.BR sscanf 
are identical to 
.BR scanf ,
other than the
source of input: 
.BR fscanf 
reads from a file, and 
.BR sscanf 
from a string.

The string at 
.BR *<[format >>
is a character sequence composed
of zero or more directives. Directives are composed of
one or more whitespace characters, non-whitespace characters,
and format specifications.

Whitespace characters are blank (
.BR   ),
tab (
.BR \t ),
or
newline (
.BR \n ).
When 
.BR scanf 
encounters a whitespace character in the format string
it will read (but not store) all consecutive whitespace characters
up to the next non-whitespace character in the input.

Non-whitespace characters are all other ASCII characters except the
percent sign (
.BR % ).
When 
.BR scanf 
encounters a non-whitespace
character in the format string it will read, but not store
a matching non-whitespace character.

Format specifications tell 
.BR scanf 
to read and convert characters
from the input field into specific types of values, and store then
in the locations specified by the address arguments.

Trailing whitespace is left unread unless explicitly
matched in the format string.

The format specifiers must begin with a percent sign (
.BR % )
and have the following form:

. %[*][
.IR width ][<[size]>]<[type]>

Each format specification begins with the percent character (
.BR % ).
The other fields are:
o+
o *
an optional marker; if present, it suppresses interpretation and
assignment of this input field.

o 
.IR width 
an optional maximum field width: a decimal integer,
which controls the maximum number of characters that
will be read before converting the current input field. If the
input field has fewer than 
.IR width 
characters, 
.BR scanf 
reads all the characters in the field, and then
proceeds with the next field and its format specification.

If a whitespace or a non-convertable character occurs
before 
.IR width 
character are read, the characters up
to that character are read, converted, and stored.
Then 
.BR scanf 
proceeds to the next format specification.

o size
.BR h ,
.BR l ,
and 
.BR L 
are optional size characters which
override the default way that 
.BR scanf 
interprets the
data type of the corresponding argument.

.Modifier Type(s)
. h d, i, o, u, x convert input to short,
. store in short object
.
. h D, I, O, U, X no effect
. e, f, c, s, n, p
.
. l d, i, o, u, x convert input to long,
. store in long object
.
. l e, f, g convert input to double
. store in a double object
.
. l D, I, O, U, X no effect
. c, s, n, p
.
. L d, i, o, u, x convert to long double,
. store in long double
.
. L all others no effect

o 
.IR type 

A character to specify what kind of conversion
.BR scanf 
performs. Here is a table of the conversion
characters:

o+
o %
No conversion is done; the percent character (
.BR % )
is stored.

o c
Scans one character. Corresponding 
.IR arg :
.BR (char *arg) .

o s
Reads a character string into the array supplied.
Corresponding 
.IR arg :
.BR (char arg[]) .

o [
.IR pattern ]
Reads a non-empty character string into memory
starting at 
.IR arg .
This area must be large
enough to accept the sequence and a
terminating null character which will be added
automatically. (
.IR pattern 
is discussed in the paragraph following
this table). Corresponding 
.IR arg :
.BR (char *arg) .

o d
Reads a decimal integer into the corresponding 
.IR arg :
.BR (int *arg) .

o D
Reads a decimal integer into the corresponding
.IR arg :
.BR (long *arg) .

o o
Reads an octal integer into the corresponding 
.IR arg :
.BR (int *arg) .

o O
Reads an octal integer into the corresponding 
.IR arg :
.BR (long *arg) .

o u
Reads an unsigned decimal integer into the corresponding
.IR arg :
.BR (unsigned int *arg) .

o U
Reads an unsigned decimal integer into the corresponding 
.IR arg :
.BR (unsigned long *arg) .

o x,X
Read a hexadecimal integer into the corresponding 
.IR arg :
.BR (int *arg) .

o e, f, g
Read a floating point number into the corresponding 
.IR arg :
.BR (float *arg) .

o E, F, G
Read a floating point number into the corresponding 
.IR arg :
.BR (double *arg) .

o i
Reads a decimal, octal or hexadecimal integer into the
corresponding 
.IR arg :
.BR (int *arg) .

o I
Reads a decimal, octal or hexadecimal integer into the
corresponding 
.IR arg :
.BR (long *arg) .

o n
Stores the number of characters read in the corresponding
.IR arg :
.BR (int *arg) .

o p
Stores a scanned pointer. ANSI C leaves the details
to each implementation; this implementation treats
.BR %p 
exactly the same as 
.BR %U .
Corresponding
.IR arg :
.BR (void **arg) .
o-

A 
.IR pattern 
of characters surrounded by square brackets can be used
instead of the 
.BR s 
type character. 
.IR pattern 
is a set of
characters which define a search set of possible characters making up
the 
.BR scanf 
input field. If the first character in the brackets is a
caret (
.BR ^ ),
the search set is inverted to include all ASCII characters
except those between the brackets. There is also a range facility
which you can use as a shortcut. 
.BR %[0-9]  
matches all decimal digits.
The hyphen must not be the first or last character in the set.
The character prior to the hyphen must be lexically less than the
character after it.

Here are some 
.IR pattern 
examples:
o+
o %[abcd]
matches strings containing only 
.BR a ,
.BR b ,
.BR c ,
and 
.BR d .

o %[^abcd]
matches strings containing any characters except 
.BR a ,
.BR b ,
.BR c ,
or 
.BR d 

o %[A-DW-Z]
matches strings containing 
.BR A ,
.BR B ,
.BR C ,
.BR D ,
.BR W ,
.BR X ,
.BR Y ,
.BR Z 

o %[z-a]
matches the characters 
.BR z ,
.BR - ,
and 
.BR a 
o-

Floating point numbers (for field types 
.BR e ,
.BR f ,
.BR g ,
.BR E ,
.BR F ,
.BR G )
must correspond to the following general form:

. [+/-] ddddd[.]ddd [E|e[+|-]ddd]

where objects inclosed in square brackets are optional, and 
.BR ddd 
represents decimal, octal, or hexadecimal digits.
o-
.SH RETURNS
.BR scanf 
returns the number of input fields successfully
scanned, converted and stored; the return value does
not include scanned fields which were not stored.

If 
.BR scanf 
attempts to read at end-of-file, the return
value is 
.BR EOF .

If no fields were stored, the return value is 
.BR 0 .

.BR scanf 
might stop scanning a particular field before
reaching the normal field end character, or may
terminate entirely.

.BR scanf 
stops scanning and storing the current field
and moves to the next input field (if any)
in any of the following situations:

O+
o The assignment suppressing character (
.BR * )
appears
after the 
.BR % 
in the format specification; the current
input field is scanned but not stored.

o 
.IR width 
characters have been read (
.IR width 
is a
width specification, a positive decimal integer).

o The next character read cannot be converted
under the the current format (for example,
if a 
.BR Z 
is read when the format is decimal).

o The next character in the input field does not appear
in the search set (or does appear in the inverted search set).
O-

When 
.BR scanf 
stops scanning the current input field for one of
these reasons, the next character is considered unread and
used as the first character of the following input field, or the
first character in a subsequent read operation on the input.

.BR scanf 
will terminate under the following circumstances:

O+
o The next character in the input field conflicts
with a corresponding non-whitespace character in the
format string.

o The next character in the input field is 
.BR EOF .

o The format string has been exhausted.
O-

When the format string contains a character sequence that is
not part of a format specification, the same character
sequence must appear in the input; 
.BR scanf 
will
scan but not store the matched characters. If a
conflict occurs, the first conflicting character remains in the input
as if it had never been read.
.SH PORTABILITY
.BR scanf 
is ANSI C.

Supporting OS subroutines required: 
.BR close ,
.BR fstat ,
.BR isatty ,
.BR lseek ,
.BR read ,
.BR sbrk ,
.BR write .
.SH SOURCE
src/newlib/libc/stdio/sscanf.c
